package com.ikkong.assets.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.ikkong.assets.service.HardwareService;
import com.ikkong.assets.service.SoftService;
import com.ikkong.assets.service.impl.HardwareServiceImpl;
import com.ikkong.assets.service.impl.SoftServiceImpl;
import com.ikkong.core.base.BaseController;
import com.ikkong.core.dao.Blade;
import com.ikkong.core.jfinal.ext.kit.JsonKit;
import com.ikkong.core.toolbox.Record;
import com.ikkong.system.controller.base.UrlPermissController;
import com.ikkong.assets.model.Source;
import com.ikkong.assets.service.SourceService;
import com.ikkong.assets.service.impl.SourceServiceImpl;

/**
 * Generated by Blade.
 * 2018-04-08 22:18:46
 */
public class SourceController extends UrlPermissController {
	private static String CODE = "source";
	private static String PERFIX = "source";
	private static String LIST_SOURCE = "Source.list";
	private static String BASE_PATH = "/platform/source/";
	
	SourceService service = new SourceServiceImpl();
	SoftService softService = new SoftServiceImpl();
	HardwareService hardwareService = new HardwareServiceImpl();
	
	public void index() {
		Integer id = getParaToInt("id");
		List<Map> softlist = Blade.dao().select("Source.softlist", Map.class, Record.create().set("proid", id));
		List<Map> hardwarelist = Blade.dao().select("Source.hardwarelist", Map.class, Record.create().set("proid", id));
		List<Map> sourcelist = Blade.dao().select("Source.sourcelist", Map.class, Record.create().set("proid", id));
		setAttr("proid",id);
		setAttr("hards",hardwarelist);
		setAttr("softs",softlist);
		setAttr("sources",sourcelist);
		setAttr("code", CODE);
		render(BASE_PATH + "source.html");
	}

	public void add() {
		String proid = getPara("id");
		setAttr("proid",proid);
		setAttr("code", CODE);
		render(BASE_PATH + "source_add.html");
	}

	public void edit() {
		String id = getPara(0);
		Source source = service.findById(id);
		setAttr("model", JsonKit.toJson(source));
		setAttr("id", id);
		setAttr("code", CODE);
		render(BASE_PATH + "source_edit.html");
	}

	public void view() {
		String id = getPara(0);
		Source source = service.findById(id);
		setAttr("model", JsonKit.toJson(source));
		setAttr("id", id);
		setAttr("code", CODE);
		render(BASE_PATH + "source_view.html");
	}

	public void list() {
		Object grid = paginate(LIST_SOURCE);
		renderJson(grid);
	}

	public void save() {
		Source source = mapping(PERFIX, Source.class);
		boolean temp = service.save(source);
		if (temp) {
			renderJson(success(SAVE_SUCCESS_MSG));
		} else {
			renderJson(error(SAVE_FAIL_MSG));
		}
	}

	public void update() {
		Source source = mapping(PERFIX, Source.class);
		boolean temp = service.update(source);
		if (temp) {
			renderJson(success(UPDATE_SUCCESS_MSG));
		} else {
			renderJson(error(UPDATE_FAIL_MSG));
		}
	}

	public void remove() {
		String ids = getPara("ids");
		int cnt = service.deleteByIds(ids);
		if (cnt > 0) {
			renderJson(success(DEL_SUCCESS_MSG));
		} else {
			renderJson(error(DEL_FAIL_MSG));
		}
	}
}
